home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / xview / guidexv / c_varieties.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-22  |  5.1 KB  |  154 lines

  1. /*    @(#)c_varieties.h    2.4 91/10/15 SMI    */
  2.  
  3. /*
  4.  *      (c) Copyright 1989 Sun Microsystems, Inc. Sun design patents
  5.  *      pending in the U.S. and foreign countries. See LEGAL NOTICE
  6.  *      file for terms of the license.
  7.  */
  8. #ifndef C_VARIETIES_H
  9. #define C_VARIETIES_H
  10.  
  11. /*
  12.  *    This file defines some macros that are used to make code
  13.  *    portable between the major C dialects currently in use at
  14.  *    Sun.  As of 12/88, these include Sun C (a lot like K&R C),
  15.  *    ANSI C, and C++.
  16.  *
  17.  * external functions:
  18.  *    To declare an external function, invoke the EXTERN_FUNCTION
  19.  *    macro; the macro's first parameter should be the function's
  20.  *    return type and function name, and the second macro parameter
  21.  *    should be the parenthesized list of function arguments (or an
  22.  *    ellipsis - DOTDOTDOT macro should be used to indicate the 
  23.  *      ellipsis as explained later in this file - if the arguments are 
  24.  *      unspecified or of varying number or type, or the uppercase word 
  25.  *      "_VOID_" if the function takes no arguments).  Some examples:
  26.  *
  27.  *        EXTERN_FUNCTION( void printf, (char *, DOTDOTDOT) );
  28.  *        EXTERN_FUNCTION( int fread, (char*, int, int, FILE*) );
  29.  *        EXTERN_FUNCTION( int getpid, (_VOID_) );
  30.  *
  31.  *    Note that to be ANSI-C conformant, one should put "," at the end
  32.  *    first argument of printf() declaration.
  33.  *
  34.  * structure tags:
  35.  *    In order to handle cases where a structure tag has the same name
  36.  *    as a type, the STRUCT_TAG macro makes the tag disappear in C++.
  37.  *    An example (from <sys/types.h>):
  38.  *
  39.  *        typedef struct STRUCT_TAG(fd_set) { ... } fd_set;
  40.  *
  41.  * enum bitfields:
  42.  *    In K&R C as interpreted at UCB, bitfields may be declared to
  43.  *    be of an enumerated type.  Neither ANSI C nor C++ permit this,
  44.  *    so the ENUM_BITFIELD macro replaces the enum declaration with
  45.  *    "unsigned".   An example (from <sunwindow/attr.h>):
  46.  *
  47.  *        struct {
  48.  *        ENUM_BITFIELD( Attr_pkg )    pkg        : 8;
  49.  *        unsigned            ordinal        : 8;
  50.  *        ENUM_BITFIELD( Attr_list_type )    list_type    : 8;
  51.  *        ...
  52.  *        };
  53.  *
  54.  * enum type specifier:
  55.  *    In K&R C, it is OK to use "enum xyz" as return type but in C++,
  56.  *     one should use "xyz".  ENUM_TYPE macro is used to handle this.
  57.  *
  58.  *        ENUM_TYPE(enum, xyz) (*func) (...);
  59.  *
  60.  * "struct s s;":
  61.  *    C++ does not allow this sort of name conflict, since struct tags are
  62.  *    in the same namespace as variables.  In this case, we use the
  63.  *    NAME_CONFLICT macro to prepend (for C++) an underscore to the
  64.  *    variable (or struct member) name.  E.g. from <pixrect/pixrect.h>:
  65.  *
  66.  *        typedef struct pixrect {
  67.  *        struct    pixrectops *pr_ops;
  68.  *        struct    pr_size NAME_CONFLICT(pr_size);
  69.  *        } Pixrect;
  70.  *        #define pr_height    NAME_CONFLICT(pr_size).y
  71.  *        #define pr_width    NAME_CONFLICT(pr_size).x
  72.  *
  73.  *    Note that no spaces are allowed within the parentheses in the
  74.  *    invocation of NAME_CONFLICT.
  75.  *
  76.  * Pointers to functions declared as struct members:
  77.  *    Instead of getting picky about the types expected by struct
  78.  *    members which are pointers to functions, we use DOTDOTDOT to
  79.  *    tell C++ not to be so uptight:
  80.  *
  81.  *        struct pixrectops {
  82.  *            int    (*pro_rop)( DOTDOTDOT );
  83.  *            int    (*pro_stencil)( DOTDOTDOT );
  84.  *            int    (*pro_batchrop)( DOTDOTDOT );
  85.  *            . . .
  86.  *        };
  87.  *
  88.  */
  89.  
  90.  
  91. /* Which type of C/C++ compiler are we using? */
  92.  
  93. #if defined(__cplusplus)
  94.     /*
  95.      * Definitions for C++ 2.0 and later require extern "C" { decl; }
  96.      */
  97. #   define EXTERN_FUNCTION( rtn, args ) extern "C" { rtn args; }
  98. #   define STRUCT_TAG( tag_name ) /* the tag disappears */
  99. #   define ENUM_BITFIELD( enum_type ) unsigned
  100. #   define ENUM_TYPE( enum_sp, enum_ty ) enum_ty
  101. #   define NAME_CONFLICT( name ) _/**/name
  102. #   define DOTDOTDOT ...
  103. #   define _VOID_ /* anachronism */
  104.  
  105. /*
  106.  * This is not necessary for 2.0 since 2.0 has corrected the void (*) () problem
  107.  */
  108. typedef void (*_PFV_)();
  109. typedef int (*_PFI_)();
  110.  
  111. #elif defined(c_plusplus)
  112.     /*
  113.      * Definitions for C++ 1.2
  114.      */
  115. #   define EXTERN_FUNCTION( rtn, args ) rtn args
  116. #   define STRUCT_TAG( tag_name )  /* the tag disappears */
  117. #   define ENUM_BITFIELD( enum_type ) unsigned
  118. #   define ENUM_TYPE( enum_sp, enum_ty ) enum_ty
  119. #   define NAME_CONFLICT( name ) _/**/name
  120. #   define DOTDOTDOT ...
  121. #   define _VOID_ /* anachronism */
  122. typedef void (*_PFV_)();
  123. typedef int (*_PFI_)();
  124.  
  125. #elif defined(__STDC__)
  126.     /*
  127.      * Definitions for ANSI C
  128.      */
  129. #   define EXTERN_FUNCTION( rtn, args ) rtn args
  130. #   define STRUCT_TAG( tag_name ) tag_name
  131. #   define ENUM_BITFIELD( enum_type ) unsigned
  132. #   define ENUM_TYPE( enum_sp, enum_ty ) enum_sp enum_ty
  133. #   define NAME_CONFLICT( name ) name
  134. #   define DOTDOTDOT ...
  135. #   define _VOID_ void
  136.  
  137. #else
  138.     /*
  139.      * Definitions for Sun/K&R C -- ignore function prototypes,
  140.      * but preserve tag names and enum bitfield declarations.
  141.      */
  142. #   define EXTERN_FUNCTION( rtn, args ) rtn()
  143. #   define STRUCT_TAG( tag_name ) tag_name
  144. #   define ENUM_BITFIELD( enum_type ) enum_type
  145. #   define ENUM_TYPE( enum_sp, enum_ty ) enum_sp enum_ty
  146. #   define NAME_CONFLICT( name ) name
  147. #   define DOTDOTDOT
  148. #   define _VOID_
  149.     /* VOID is only used where it disappears anyway */
  150.  
  151. #endif /* Which type of C/C++ compiler are we using? */
  152.  
  153. #endif /* ~defined(C_VARIETIES_H) */
  154.